import cookie from './util.cookies';
import store from "@/store";
import {Base64} from 'js-base64';

const token = {};

token.initToken = function () {
  let tokenStr = cookie.get('access_token')
  if (!tokenStr || tokenStr == '-1') { return false }
  let userMessage="";
  try{
    userMessage = Base64.decode(tokenStr.split('.')[1]).replace(/\'/g, '')
  }catch(err){
    console.log(err);
  }

  userMessage = userMessage.substring(0, userMessage.indexOf("}") + 1)
  userMessage = JSON.parse(userMessage)
  let subUser = store.state.admin.subUser.user
  if (subUser.custId) {
    userMessage.custId = subUser.custId
  }
  if (subUser.cliqueId) {
    userMessage.cliqueId = subUser.cliqueId
  }
  if (subUser.groupCode) {
    userMessage.groupCode = subUser.groupCode
  }
  return userMessage
}

export default token;
